//
//  Emotion.swift
//  emotionKeyBoard
//
//  Created by 路峻鉴 on 15/10/17.
//  Copyright © 2015年 路峻鉴. All rights reserved.
//

import UIKit

//表情包模型
class emotionPackage: NSObject {
    /// 目录名
    var id: String
    /// 分组名
    var groupName: String = ""
    /// 表情数组
    var emoticons: [Emotion]?
    init(id: String, groupName: String = "") {
        self.id = id
        self.groupName = groupName
    }
    
    ///加载所有表情包
    class func packages() -> [emotionPackage] {
        //包路径
        let path = bundlePath.stringByAppendingString("/emoticons.plist")
        //加载plist
        let dic = NSDictionary(contentsOfFile: path)!
        let array = dic["packages"] as! [[String: AnyObject]]
        //遍历数组
        var arrayM = [emotionPackage]()
        let emotionPack = emotionPackage.init(id: "", groupName: "最近").appendEmptyEmotions()
        arrayM.append(emotionPack)
        for dict in array {
            let id = dict["id"] as! String
            let package = emotionPackage.init(id: id).loadEmotions().appendEmptyEmotions()
            arrayM.append(package)
            
        }
        return arrayM
    }
    ///填充空白表情和删除按钮
    func appendEmptyEmotions() -> Self {
        // 判断表情数组是否存在
        if emoticons == nil {
            emoticons = [Emotion]()
        }
        
        // 判断是否整页
        let count = emoticons!.count % 21
        
        // count == 0 不需要填充
        if count > 0 || emoticons!.count == 0 {
            for _ in count..<20 {
                emoticons?.append(Emotion(remove: false))
            }
            // 最末尾追加一个删除按钮
            emoticons?.append(Emotion(remove: true))
        }

        return self
    }
    ///完善本身信息
    private func loadEmotions() ->Self {
        //路径
        let path = emotionPackage.bundlePath.stringByAppendingString("/\(id)").stringByAppendingString("/info.plist")
        let dict = NSDictionary(contentsOfFile: path)!
        
        groupName = dict["group_name_cn"] as! String
        let array = dict["emoticons"] as! [[String: String]]
        //遍历
        emoticons = [Emotion]()
        var index = 0
        for d in array {
            emoticons?.append(Emotion(id: id, dict: d))
            index++
            // 插入删除表情
            if index == 20 {
                emoticons?.append(Emotion(remove: true))
                index = 0
            }
        }
        
        return self
    }
    
    ///表情包路径
    static let bundlePath = NSBundle.mainBundle().bundlePath.stringByAppendingString("/Emoticons.bundle")
    
    //描述方法重写
    override var description: String {
        return "->\(id) ->\(groupName) ->\(emoticons)"
    }
}

class Emotion: NSObject {
    /// 表情目录
    var id: String?
    
    /// 发送给服务器的表情文字
    var chs: String?
    /// 本地显示的图片文件名
    var png: String?
    /// emoji 的代码
    var code: String? {
        didSet {
            // 扫描器
            let scanner = NSScanner(string: code!)
            
            var value: UInt32 = 0
            scanner.scanHexInt(&value)
            
            emoji = String(Character(UnicodeScalar(value)))
        }
    }
    /// emoji 字符串
    var emoji: String?
    /// 图片完整路径
    var imagePath: String {
        // 判断是否是`图片`表情
        if chs == nil {
            return ""
        }
        return emotionPackage.bundlePath.stringByAppendingString("/\(id!)").stringByAppendingString("/\(png!)")
    }
    
    /// 删除表情标记
    var removeEmoticon = false
    ///创建删除表情
    init(remove: Bool) {
        removeEmoticon = remove
    }
    
    init(id: String, dict: [String: String]) {
        super.init()
        self.id = id
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let str = imagePath == "" ? "空路径" : "有路径"
        return "\(chs) \(png) \(emoji)" + str
    }
}
